package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        // 判断加入的商品是否已经在购物车中
        // 创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 将购物车DTO对象的属性复制到购物车对象
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取当前用户ID
        Long userId = BaseContext.getCurrentId();
        // 设置购物车的用户ID
        shoppingCart.setUserId(userId);

        // 查询购物车列表
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 如果购物车列表不为空且存在记录
        if (list != null && list.size() > 0){
            // 获取第一个购物车记录
            ShoppingCart cart = list.get(0);
            // 将数量加1
            cart.setNumber(cart.getNumber() + 1);
            // 更新购物车数量
            shoppingCartMapper.updateNumberById(cart);
        }
        // 如果不在购物车中，添加到购物车中
        else {
            // 判断当前插入购物车的是不是菜品
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                // 根据菜品ID获取菜品信息
                Dish dish = dishMapper.getById(dishId);
                // 设置购物车的菜品名称、图片和金额
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            else {
                // 获取套餐ID
                Long setmealId = shoppingCartDTO.getSetmealId();
                // 根据套餐ID获取套餐信息
                Setmeal setmeal = setmealMapper.getById(setmealId);
                // 设置购物车的套餐名称、图片和金额
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 设置购物车数量为1
            shoppingCart.setNumber(1);
            // 设置购物车创建时间为当前时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 插入购物车记录
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 减少购物车
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取当前用户ID
        Long userId = BaseContext.getCurrentId();
        // 设置购物车的用户ID
        shoppingCart.setUserId(userId);
        // 查询购物车列表
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        // 有商品的话就数量-1
        if (list != null && list.size() > 0) {
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            Long shoppingCartId = shoppingCart.getId();
            if (number == 1) {
                shoppingCartMapper.deleteById(shoppingCartId);
            }else {
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }



    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
